import oss2
import time

OSS_ACCESS_KEY_ID = 'LTAI5tRabCiPft7fKywGysvL'
OSS_ACCESS_KEY_SECRET = 'YYx4hShu1PijNE71AmreSOONfcNQiv'
OSS_ENDPOINT = 'oss-cn-hangzhou.aliyuncs.com'  # e.g., 'https://oss-region.aliyuncs.com'
OSS_BUCKET_NAME = 'plenty-1'
OSS_BUCKET_NAME_DOMAIN = 'https://plt.hfplenty.com'

auth = oss2.Auth(OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET)
bucket = oss2.Bucket(auth, OSS_ENDPOINT, OSS_BUCKET_NAME)


def upload_to_oss(file):
    '''
    上传阿里云oss
    :param file:
    :return:
    '''
    if not file:
        raise ValueError("No file provided")
    file_name_prefix = str(int(time.time())) + "_"
    object_name = f'uploads/{file_name_prefix}{file.filename}'
    try:
        bucket.put_object(object_name, file)
        return f"{OSS_BUCKET_NAME_DOMAIN}/{object_name}"
    except Exception as e:
        raise RuntimeError(f"Failed to upload to OSS: {str(e)}")
